package gamebackend;

import java.util.Random;

import framework.Point;

/**
 * Repeatedly makes short turns followed by straight-aways.
 * @author Jonathan Caddey
 *
 */
public class StutterFlier extends AbstractFlier {

	private boolean my_goingStraight = true;
	private double my_turnLength;
	private double my_straightLength;
	/**
	 * By how much the ship will move.
	 */
	private double my_change;
	
	public StutterFlier(final Flier the_child, final double the_change) {
		super(the_child);
		my_change = the_change;
	}
	@Override
	public void step(final double the_yChange) {
		super.step(the_yChange);
		if (my_goingStraight) {
			my_straightLength += the_yChange;
			if (my_straightLength > 1.5 * my_turnLength) {
				my_goingStraight = false;
				my_turnLength = 0;
				double deltaX;
				double difference = Math.abs(getChild().getPosition() - getTarget());
				if (difference > my_change) {
					deltaX = my_change;
				} else {
					deltaX = difference;
				}
				if (getTarget() < getChild().getPosition()) {
					deltaX *= -1;
				}
				getChild().setTarget(getChild().getPosition() + deltaX);
				
			}
			
		} else {
			my_turnLength += the_yChange;
			if (getChild().isAtTarget()) {
				my_goingStraight = true;
				my_straightLength = 0;
			}
			
		}
		
		
	}

	/**
	 * @param the_width how far the ship can fly horizontally before it 
	 * flies straight vertically.
	 */
	public void setStutterWidth(final double the_width) {
		my_change = the_width;
	}
	public double getStutterWidth() {
		return my_change;
	}
	@Override
	public boolean isAtTarget() {
		return getTarget() == getChild().getTarget() && getChild().isAtTarget();
	}

}
